/**
 * Break语句导致 while 循环、do-while 循环、for 循环或 switch 语句结束，并在循环或 switch 被中断后继续执行下一个语句。
 * 
 * 最佳实践
 *   当中断和继续简化循环逻辑时，请使用它们。
 *   当早期返回可以简化函数逻辑时，请使用它们。
 * 
 */

#include <iostream>

int breakOrReturn()
{
    while (true) // infinite loop
    {
        std::cout << "Enter 'b' to break or 'r' to return: ";
        char ch{};
        std::cin >> ch;

        if (ch == 'b')
            break; // execution will continue at the first statement beyond the loop

        if (ch == 'r')
            return 1; // return will cause the function to immediately return to the caller (in this case, main())
    }

    // breaking the loop causes execution to resume here

    std::cout << "We broke out of the loop\n";

    return 0;
}

int main()
{
    int returnValue{ breakOrReturn() };
    std::cout << "Function breakOrReturn returned " << returnValue << '\n';

    return 0;
}